package com.future.common.api.result;

import com.future.common.api.content.ResultConstant;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;

/**
 * @ClassName : R
 * @Author : lby
 * @Date: 2020/11/18 16:56
 * @Description : 公共返回类
 */
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    @Getter
    @Setter
    private int code;

    @Getter
    @Setter
    private String msg;

    @Getter
    @Setter
    private Boolean state;


    @Getter
    @Setter
    private T data;

    public static <T> R<T> ok() {
        return restResult(null, ResultConstant.SUCCESS, null, true);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, ResultConstant.SUCCESS, null, true);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, ResultConstant.SUCCESS, msg, true);
    }

    public static <T> R<T> failed() {
        return restResult(null, ResultConstant.FAIL, null, false);
    }

    public static <T> R<T> failed(String msg) {
        return restResult(null, ResultConstant.FAIL, msg, false);
    }

    public static <T> R<T> failed(T data) {
        return restResult(data, ResultConstant.FAIL, null, false);
    }

    public static <T> R<T> failed(T data, String msg) {
        return restResult(data, ResultConstant.FAIL, msg, false);
    }

    private static <T> R<T> restResult(T data, int code, String msg, Boolean state) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setState(state);
        return apiResult;
    }
}
